<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ChangeStatus;
use App\Models\FinancialInstitution;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class FinancialInstitutionController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FinancialInstitution(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('description');
            $grid->column('type')->label('warning');
            $grid->column('address');
            $grid->column('phone');
            $grid->column('contact');
            $grid->column('registration');
            $grid->column('status')->using([0 => '停业', 1 => '开业'])
                ->label([
                    0 => 'gray',
                    1 => 'green'
                ]);
            $grid->column('updated_at')->sortable();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->prepend(new ChangeStatus(FinancialInstitution::class), '停业/开业');
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new FinancialInstitution(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('description');
            $show->field('type')->label('warning');
            $show->field('address');
            $show->field('phone');
            $show->field('contact');
            $show->field('qualification')->image();
            $show->field('registration');
            $show->field('status')->using([0 => '停业', 1 => '开业'])->label();
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new FinancialInstitution(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->text('type');
            $form->textarea('description');
            $form->multipleImage('qualification')->autoUpload()->saveFullUrl()
                ->saving(function ($v) {
                    return implode(',', $v);
                });
            $form->text('address');
            $form->text('contact')->width(2);
            $form->mobile('phone')->options(['mask' => '999 9999 9999']);
            $form->date('registration');
            $form->radio('status')->options([0 => '停业', 1 => '开业'])->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
